Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Datatypes

Range datatype

Range in python

In Python, the range type represents an efficient sequence of whole numbers (integers). While it looks like it might store the entire sequence in memory, the range type has a special behavior that makes it incredibly memory-efficient, especially when dealing with large sequences. Think of it as a blueprint for creating numbers on demand rather than a full, pre-generated list. Key Properties of rangeOptimized Representation: The range type doesn't explicitly store every number in the sequence. Instead, it records the start, stop, and step values. It generates numbers on the fly during iteration. This saves significant memory compared to storing all values up front. ✦ Immutability: A range object, once created, is immutable. You can't change its start, stop, or step values directly. ✦ Usage with for loops: The range type is primarily designed for use with for loops, making it easy to iterate over numeric sequences.

Creating range Objects

You use the range() function to create range objects. Here's the basic structure:
range structure and syntax range(start, stop, step)
start (optional): The starting integer of the sequence. Defaults to 0. stop (required): The end point of the sequence (the sequence does not include this number). step (optional): The difference between consecutive numbers in the sequence. Defaults to 1 for increasing order and -1 for decreasing order.
Printing first 5 values using range and for loop for i in range(5): print(i)

Output

0 1 2 3 4
Generating lists of numbers (using the list constructor):
Generating list using range with list constructor numbers = list(range(1, 11)) print(numbers)

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Why Choose range?

Memory Efficiency: Ideal for working with very large sequences that would be impractical to store entirely in memory as a list. Clean syntax: The range() function offers a simple and readable way to define numerical sequences.

Examples of the range data type in Python:

1. Generating a sequence of numbers:
generating a sequence of numbers between two numbers using range in python # Create a range from 3 to 8 (excluding 8) for j in range(3, 8): print(j)

Output

3 4 5 6 7
2. Using range with a step value:
Using range with a step value for l in range(10, 0, -1): print(l)

Output

10 9 8 7 6 5 4 3 2 1
3. Creating a list from a range:
Slicing a range to create a sub-list example in python # Convert a range to a list numbers = list(range(5)) sub_list = list(range(5, 10)) print(numbers,sub_list)

Output

[0, 1, 2, 3, 4] [5, 6, 7, 8, 9]
4. Using range as a generator expression:
Generate a list of squares using range and list comprehension squares = [i**2 for i in range(5)] print(squares)

Output

[0, 1, 4, 9, 16]
These examples showcase the flexibility and memory-efficiency of the range data type in Python.

More examples - Using range in a for loop:

Creating a range from 0 to 9:

Creating a range from 0 to 9 in python basic example my_range = range(10) for i in my_range: print(i)

Output

0 1 2 3 4 5 6 7 8 9

Generating even numbers from 2 to 10:

Generating even numbers from 2 to 10 in python using range datatype even_numbers = range(2, 11, 2) for i in even_numbers: print(i)

Output

2 4 6 8 10

Creating a sequence in descending order:

Creating a sequence in descending order using range datatype in python countdown = range(10, 0, -1) for i in countdown: print(i)

Output

10 9 8 7 6 5 4 3 2 1

Summary

✦ Creation: You can create a range by calling the range() function with one, two, or three arguments. ✦ range(stop): Creates a range of numbers from 0 up to, but not including, stop. ✦ range(start, stop): Creates a range of numbers from start up to, but not including, stop. ✦ range(start, stop, step): Creates a range of numbers from start up to, but not including, stop, incrementing by step. ✦ Usage: The range data type is often used in for loops to repeat an operation a certain number of times. For example, for i in range(5): print(i) would print the numbers 0 through 4. ✦ Characteristics: The range data type only works with integers. All three arguments (start, stop, step) must be integers. They can be either positive or negative. The step argument cannot be zero; if it is, Python will raise a ValueError. Remember, Python uses the range data type to improve the readability of code and make it consistent across the wide spectrum of Python code. Consistency within one module or function is the most important.

  📌TAGS

★python ★ datatypes ★ range ★ for

Tutorials